﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PivotBeer
{
    //Name, BA Overall Rating, Brewed By, Brewery Location, Style, ABV

    public static class StringExt
    {
        public static string StringBetween(this string data, string before, string after)
        {
            try
            {

                int stringStart = data.IndexOf(before);

                if (stringStart == -1)
                    return null;

                int stringStop = data.IndexOf(after, stringStart + before.Length);

                if (stringStop == -1)
                    return null;

                return data.Substring(stringStart + before.Length, stringStop - (stringStart + before.Length));
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

    class Beer
    {
        public Beer()
        {
        }

        public Beer(string filename)
        {
            SourceURL = filename;
            StreamReader sr = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read));
            ConstructFromString(sr.ReadToEnd());
            sr.Close();
        }

        public Beer(string sourceurl, string data)
        {
            SourceURL = sourceurl;
            ConstructFromString(data);
        }

        public void Debug()
        {
            Console.WriteLine(BeerName);
            Console.WriteLine(Rating);
            Console.WriteLine(ReviewCount + " reviews");
            Console.WriteLine(StyleID + " style");
            Console.WriteLine(AlcContent + " percent");
            Console.WriteLine(RegionStateProvince);
            Console.WriteLine(Country);
            Console.WriteLine(Brewery);
            Console.WriteLine(BreweryLink);
            Console.WriteLine(Style);
            Console.WriteLine(ID + " is the id");
            Console.WriteLine(Link);
        }

        void ConstructFromString(string data)
        {
            BeerName = data.StringBetween("class=\"norm\">", "</h1>");
            Rating = data.StringBetween("<span class=\"BAscore_big\">", "</span>");
            ReviewCount = int.Parse(data.StringBetween("<br>w/ ", " Reviews</td>").Replace(",",""));
            StyleID = int.Parse( data.StringBetween("<a href=\"/beer/style/", "\">") );
            AlcContent = double.Parse(data.StringBetween("</a> | &nbsp;", "% <a"));
            string locStr = data.StringBetween("<a href=\"/beerfly/directory/", "\">");
            string[] locArr = locStr.Split('/');
            RegionStateProvince = data.StringBetween("<a href=\"/beerfly/directory/" + locStr + "\">", "</a>");
            Country = data.StringBetween("<a href=\"/beerfly/directory/" + locArr[0] + "/" + locArr[1] + "\">", "</a>");
            string profID = data.StringBetween("Brewed by:</b><br><a href=\"/beer/profile/", "\">");
            Brewery = data.StringBetween("href=\"/beer/profile/" + profID + "\"><b>", "</b>");
            BreweryLink = data.StringBetween("<b>" + Brewery + "</b></a>&nbsp;<a href=\"", "\">");
            Style = data.StringBetween("href=\"/beer/style/" + StyleID + "\"><b>", "</b>");
            ID = int.Parse(data.StringBetween("/beer/profile/" + profID + "/", "/"));
            Link = "http://beeradvocate.com/beer/profile/" + profID + "/" + ID;
        }

        


        string SourceURL;
        
        [PivotField(PivotType.String)]
        public string BeerName;

        [PivotField(PivotType.String)]
        public string Rating;

        [PivotField(PivotType.Number)]
        public int ReviewCount;

        public int StyleID;

        [PivotField(PivotType.Number)]
        public double AlcContent;

        [PivotField(PivotType.String)]
        public string RegionStateProvince;

        [PivotField(PivotType.String)]
        public string Country;

        [PivotField(PivotType.String)]
        public string Brewery;
        public string BreweryLink;

        [PivotField(PivotType.String)]
        public string Style;
        public int ID;
        public string Link;



        
    }
}
